package com.neuedu.javase.jdbc.dao;

import com.neuedu.javase.jdbc.entity.Register;
import org.junit.Test;

import java.sql.SQLException;
import java.util.List;

import static org.junit.Assert.*;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-13 12:21
 * 描述 :
 */
public class RegisterDaoTest {
    RegisterDao registerDao = new RegisterDao();

    @Test
    public void insert() {
        String sql ="INSERT INTO `register` ( `name`, `gender`, `idno`, `birthday` , `age`, `address`, `book`, `visittime` ) VALUES  ( ?,?,?,?,?,?,?,? ) ";
        Object[]  params ={"使用工具类挂号的",  1, "idno",  "2010-01-02",  35,  "address",   0,  "2022-10-14"};
        try {
            int insert = registerDao.insert(sql, params);
            System.out.println("insert = " + insert);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void update() {
        String sql ="update register set idno = ? where id= ? ";
        Object[]  params ={"87978798796879687987",2};
        try {
            int update = registerDao.update(sql, params);
            System.out.println("update = " + update);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void delete() {
        int id = 3;

        try {
            int delete = registerDao.deleteById(id);
            System.out.println("delete = " + delete);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void selectPage() {

        try {
            String sql =" select * from register ";
            long total = registerDao.selectCount(sql);
            System.out.println("total = " + total);


            int current = 1;
            int size = 1;
            List<Register> list = registerDao.selectPage(sql, current, size);
            System.out.printf("总条数:%d ,当前页号：%d, 每页显示条数:%d\r\b",total,current,size);
            list.forEach(System.out::println);

            current = 2;
             list = registerDao.selectPage(sql, current, size);
            System.out.printf("总条数:%d ,当前页号：%d, 每页显示条数:%d\r\n",total,current,size);
            list.forEach(System.out::println);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }



    @Test
    public void selectList() {

        try {
            String sql =" select * from register ";

            List<Register> list = registerDao.selectList(sql);
            list.forEach(System.out::println);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void selectOne() {

        try {
            int id = 1 ;

            System.out.println("id = " + id);
            Register register = registerDao.selectById(id);
            System.out.println("register = " + register);


            id = 2;
            System.out.println("id = " + id);
            register = registerDao.selectById(id);
            System.out.println("register = " + register);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}